/**
 * @description 基础 url
 */
let baseUrl = "https://llg.devlop.comeinvip.com";
export default {
  /**
   * get 请求
   * @throws Error
   * @param {string} url 请求地址
   * @param {unknown} [params] 请求对象
   * @param {unknown} [options] 需要覆盖的选项参数
   */
  async get(url, params, options) {
    return new Promise(async (resolve, reject) => {
      try {
        let res = await uni.request({
          url: `${baseUrl}${url}`,
          data: params,
          method: "get",
          header: {
            "Content-Type": "application/json",
          },
          ...options,
        });
        if (res.statusCode !== 200) {
          return reject(new Error("网络异常，请稍后重试"));
        }
        if (res.data.status !== 1) {
          return reject(new Error("网络异常，请稍后重试"));
        }
        return resolve(res.data.data);
      } catch (error) {
        return reject(new Error("网络异常，请稍后重试"));
      }
    });
  },
  /**
   * post请求
   * @throws Error
   * @param {string} url 请求地址
   * @param {unknown} [data] 请求对象
   * @param {unknown} [options] 需要覆盖的选项参数
   */
  post(url, data, options) {
    return new Promise(async (resolve, reject) => {
      try {
        let res = await uni.request({
          url: `${baseUrl}${url}`,
          data,
          method: "post",
          header: {
            "Content-Type": "application/json",
          },
          ...options,
        });
        if (res.statusCode !== 200) {
          return reject(new Error("网络异常，请稍后重试"));
        }
        if (res.data.status !== 1) {
          return reject(new Error("网络异常，请稍后重试"));
        }
        return resolve(res.data.data);
      } catch (error) {
        return reject(new Error("网络异常，请稍后重试"));
      }
    });
  },
};
